<?php

namespace Modules\Order\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

use Modules\Core\Channels\MiniappSubscribeMessageChannel;

use Auth;
use Miniapp;
use Setting;

class OrderDelivered extends Notification
{
    use Queueable;

    public $order;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        $channels = [
            MiniappSubscribeMessageChannel::class
        ];

        return $channels;
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }

    /**
     * 获取通知的 Nexmo / 短信展示方式
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
        ->content('Your SMS message content');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    public function toMiniappSubscribeMessage($notifiable)
    {
        $order = $this->order;
        $page = 'pages/orderDetail/index?uuid=' . $order->uuid;

        $deliverRecord = $order->deliver_record;

        $expressInc = $deliverRecord->express_name ?: '无需物流';

        // if ($deliverRecord->type == 1) {
        //     $expressInc = $deliverRecord->express_name;
        // }

        $remark = $deliverRecord->remark ?: '前往小程序查看订单详情哦~';

        $message = [
            'page' => $page,
            'data' => [
                'date3' => [
                    'value' => (String)$deliverRecord->created_at
                ],
                'thing7' => [
                    'value' => $expressInc
                ],
                'character_string1' => [
                    'value' => $order->number
                ],
                'thing5' => [
                    'value' => $order->title()
                ],
                'thing8' => [
                    'value' => $remark
                ],
            ],
        ];

        $miniapp = Miniapp::get('default');

        return [
            $miniapp,
            'order_delivered',
            $message
        ];
    }
}
